package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.entity.dto.SysNoticeSetDTO;
import com.xinsoft.entity.dto.SysNoticeSetPageParam;
import com.xinsoft.entity.po.SysAction;
import com.xinsoft.entity.po.SysNoticeSet;
import com.xinsoft.entity.po.SysNoticeUser;
import com.xinsoft.entity.vo.SysNoticeUserVo;
import com.xinsoft.mapper.SysNoticeSetMapper;
import com.xinsoft.mapper.SysNoticeUserMapper;
import com.xinsoft.service.SysActionService;
import com.xinsoft.service.SysNoticeSetService;
import com.xinsoft.service.SysNoticeUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author hsm
* @description 针对表【sys_notice_set(通知设置表)】的数据库操作Service实现
* @createDate 2023-05-30 16:18:10
*/
@Service
public class SysNoticeSetServiceImpl extends ServiceImpl<SysNoticeSetMapper, SysNoticeSet>
    implements SysNoticeSetService{

    @Resource
    private SysActionService sysActionService;

    @Resource
    private SysNoticeUserService sysNoticeUserService;

    @Resource
    private SysNoticeUserMapper sysNoticeUserMapper ;


    @Override
    public SysNoticeSet addNoticeSet(SysNoticeSetDTO sysNoticeSetDTO) {
        //保存通知设置
        SysNoticeSet entity = new SysNoticeSet();
        BeanUtils.copyProperties(sysNoticeSetDTO,entity);
        this.save(entity);

        //保存人员设置
        List<SysNoticeUser> list = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(sysNoticeSetDTO.getSysNoticeUserList())){
            for (SysNoticeUserVo sysNoticeUser : sysNoticeSetDTO.getSysNoticeUserList()) {
                sysNoticeUser.setNoticeSetId(entity.getId());
                list.add(sysNoticeUser);
            }
        }
        sysNoticeUserService.saveBatch(list);
        return entity;
    }

    @Override
    public SysNoticeSet editNoticeSet(SysNoticeSetDTO sysNoticeSetDTO) {
        //保存通知设置
        SysNoticeSet entity = new SysNoticeSet();
        entity.setId(sysNoticeSetDTO.getId());
        BeanUtils.copyProperties(sysNoticeSetDTO,entity);
        this.updateById(entity);

        //删除人员设置

        List<SysNoticeUser> list = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_set_id",sysNoticeSetDTO.getId());
        sysNoticeUserService.remove(queryWrapper);

        //保存人员设置
        if(CollectionUtil.isNotEmpty(sysNoticeSetDTO.getSysNoticeUserList())){
            for (SysNoticeUser sysNoticeUser : sysNoticeSetDTO.getSysNoticeUserList()) {
                sysNoticeUser.setNoticeSetId(entity.getId());
                list.add(sysNoticeUser);
            }
        }
        sysNoticeUserService.saveBatch(list);
        return entity;
    }

    @Override
    public IPage<SysNoticeSetDTO> findPageListByParam(SysNoticeSetPageParam param) {

        //分页
        Page<SysNoticeSetDTO> page = new Page<SysNoticeSetDTO>(param.getCurrentPage(), param.getPageSize());
        //查询分页
        IPage<SysNoticeSetDTO> sets = this.baseMapper.findListByParam(page, param);
        //返回值
        return sets;
    }

    @Override
    public void del(List<Integer> ids) {
        UpdateWrapper<SysNoticeSet> sysNoticeSetUpdateWrapper = new UpdateWrapper<>();
        sysNoticeSetUpdateWrapper.set("do_delete",1);//启用
        sysNoticeSetUpdateWrapper.in("id",ids);
        this.update(sysNoticeSetUpdateWrapper);
    }

    @Override
    public SysNoticeSetDTO getSysNoticeSet(Integer id) {
        SysNoticeSet noticeSet =  this.getById(id);

        SysAction action =  sysActionService.getById(noticeSet.getActionId());

        SysNoticeSetDTO noticeSetDTO = new SysNoticeSetDTO();
        BeanUtils.copyProperties(noticeSet,noticeSetDTO);
        noticeSetDTO.setId(id);
        if(action!=null){
            noticeSetDTO.setActionName(noticeSetDTO.getActionName());
        }

        //人员或部门设置
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_set_id",id);
        noticeSetDTO.setSysNoticeUserList(sysNoticeUserMapper.selectListByParam(queryWrapper));

        return noticeSetDTO;
    }

    @Override
    public List<SysNoticeSetDTO> getSysNoticeSetByActionId(Integer actionId){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        queryWrapper.eq("action_id",actionId);
        List<SysNoticeSet> list = list(queryWrapper);
        List<SysNoticeSetDTO> result = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(list)){
            SysNoticeSetDTO getSysNoticeSet =getSysNoticeSet(list.get(0).getId());
            result.add(getSysNoticeSet);
        }
        return result;
    }

    @Override
    public List<SysAction> getNodeActionList(String actionName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(actionName),"action_name",actionName);
        queryWrapper.isNotNull("node_id");
        queryWrapper.eq("action_status",1);
        return sysActionService.list(queryWrapper);
    }

    @Override
    public List<SysAction> getCustomActionList(String actionName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(actionName),"action_name",actionName);
        queryWrapper.isNull("node_id");queryWrapper.isNull("node_id");
        queryWrapper.eq("action_status",1);
        return sysActionService.list(queryWrapper);
    }
}